{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loading\n",
      "add word2vec finished....\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/env python\n",
    "# coding: utf-8\n",
    "\n",
    "# In[1]:\n",
    "\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "import os\n",
    "import json\n",
    "from gensim.models import Word2Vec\n",
    "from tensorflow.keras.layers import (Bidirectional,\n",
    "                                     Embedding,\n",
    "                                     GRU, \n",
    "                                     GlobalAveragePooling1D,\n",
    "                                     GlobalMaxPooling1D,\n",
    "                                     Concatenate,\n",
    "                                     SpatialDropout1D,\n",
    "                                     BatchNormalization,\n",
    "                                     Dropout,\n",
    "                                     Dense,\n",
    "                                     Activation,\n",
    "                                     concatenate,\n",
    "                                     Input,\n",
    "                                     Reshape,\n",
    "                                     LSTM\n",
    "                                    )\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "from tensorflow.keras.utils import to_categorical    \n",
    "import time\n",
    "import re\n",
    "import jieba\n",
    "from gensim.models import Word2Vec\n",
    "# In[2]:\n",
    "\n",
    "def cut_words(sentence):\n",
    "    #print sentence\n",
    "    return \" \".join(jieba.cut(sentence))\n",
    "\n",
    "def build_model(sent_length, embeddings_weight):\n",
    "    content = Input(shape=(sent_length,), dtype='int32')\n",
    "\n",
    "    embedding = Embedding(\n",
    "        name=\"word_embedding\",\n",
    "        input_dim=embeddings_weight.shape[0],\n",
    "        weights=[embeddings_weight],\n",
    "        output_dim=embeddings_weight.shape[1],\n",
    "        trainable=False)\n",
    "    x = embedding(content)\n",
    "\n",
    "    x = LSTM(128)(x)\n",
    "    x = Dense(embedding_matrix.shape[0])(x)\n",
    "    output = Activation(activation=\"softmax\")(x)\n",
    "\n",
    "    model = tf.keras.models.Model(inputs=content, outputs=output)\n",
    "    model.compile(loss='categorical_crossentropy', \n",
    "                  optimizer='adam', \n",
    "                  metrics=['accuracy'])\n",
    "    return model\n",
    "\n",
    "def preprocess(inputs,labels):\n",
    "    #最简单的预处理函数:\t转numpy为Tensor、分类问题需要处理label为one_hot编码、处理训练数据\n",
    "    #把numpy数据转为Tensor\n",
    "    labels = tf.cast(labels, dtype=tf.int32)\n",
    "    #labels 转为one_hot编码\n",
    "    labels = tf.one_hot(labels, depth=embedding_matrix.shape[0])\n",
    "    return inputs,labels\n",
    "\n",
    "def sample(preds, temperature=1.0):\n",
    "    # helper function to sample an index from a probability array\n",
    "    preds = np.asarray(preds).astype('float64')\n",
    "    preds = np.log(preds) / temperature\n",
    "    exp_preds = np.exp(preds)\n",
    "    preds = exp_preds / np.sum(exp_preds)\n",
    "    probas = np.random.multinomial(1, preds, 1)\n",
    "    return np.argmax(probas)\n",
    "\n",
    "\n",
    "MAX_SEQUENCE_LENGTH = 100\n",
    "maxlen = 40\n",
    "checkpoint_dir = './checkpoints_reply'\n",
    "\n",
    "file_name = './word2vec/Word2Vec_word_200.model'\n",
    "print('loading')\n",
    "model_word2vec = Word2Vec.load(file_name)\n",
    "print(\"add word2vec finished....\")\n",
    "json_str= json.load(open('tokenizer_config.json', 'r'))\n",
    "tokenizer = tf.keras.preprocessing.text.tokenizer_from_json(json_str)\n",
    "word_vocab = tokenizer.word_index\n",
    "embedding_matrix = np.load('./idsMatrix.npy')\n",
    "word_vocal_reverse = {}\n",
    "word_vocal_reverse[0] = 'SPACE'\n",
    "\n",
    "for i,word in word_vocab.items():\n",
    "    word_vocal_reverse[word] = i\n",
    "\n",
    "model = build_model(maxlen, embedding_matrix)\n",
    "\n",
    "model.load_weights(tf.train.latest_checkpoint(checkpoint_dir))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_reply(text):\n",
    "    text = cut_words(text)\n",
    "    text_array = []\n",
    "    text_array.append(text)\n",
    "    # text_array.append(text)\n",
    "    # print(text_array)\n",
    "    a = pd.DataFrame(text_array)\n",
    "    a = a.rename(columns={0:'sentence'})\n",
    "    # p_col=['省份','id','编码']\n",
    "    # province.columns=p_col\n",
    "    print(a)\n",
    "    sequence = tokenizer.texts_to_sequences(a['sentence'].values)\n",
    "    print(sequence)\n",
    "\n",
    "    sentences = []\n",
    "\n",
    "    input = tf.keras.preprocessing.sequence.pad_sequences(sequence, maxlen=maxlen,\n",
    "                                                  padding='pre', truncating='pre', value=0.0)\n",
    "    print(input)\n",
    "    print(\"原文\\n\"+text)\n",
    "    #     for diversity in [0.2, 0.5, 1.0, 1.2]:\n",
    "    replys = []\n",
    "    for diversity in [0.2, 1.0, 1.2]:\n",
    "        print('----- diversity:', diversity)\n",
    "\n",
    "        generated = ''\n",
    "        print('----- Generating with seed:')\n",
    "        for i in range(20):#400\n",
    "            # x_pred = np.zeros((1, maxlen))\n",
    "            # for t, char in enumerate(sentence):\n",
    "            #     #print(t,char)\n",
    "            #     #x_pred[0, t, char_indices[char]] = 1.\n",
    "            #     x_pred[0, t] = char\n",
    "\n",
    "            preds = model.predict(input, verbose=0)[0]\n",
    "            next_index = sample(preds, diversity)\n",
    "            next_char = word_vocal_reverse[next_index]\n",
    "            generated += next_char\n",
    "            sequence = sequence[1:]\n",
    "            sequence.append(next_index)\n",
    "        print(generated)\n",
    "        replys.append(generated)\n",
    "    return replys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  sentence\n",
      "0    武汉 加油\n",
      "[[18, 43]]\n",
      "[[ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0\n",
      "   0  0  0  0  0  0  0  0  0  0  0  0  0  0 18 43]]\n",
      "原文\n",
      "武汉 加油\n",
      "----- diversity: 0.2\n",
      "----- Generating with seed:\n",
      "！#！！！！！####?#中国######\n",
      "----- diversity: 1.0\n",
      "----- Generating with seed:\n",
      "向:钟南山墨染看加油！这样安徽啊加油一点但是炖锅中国?不#没有到\n",
      "----- diversity: 1.2\n",
      "----- Generating with seed:\n",
      "粉#招式#这次5这场4志愿者加油我们　人员热干面真相再\"学堂我戳\n"
     ]
    }
   ],
   "source": [
    "# print(len(word_vocal_reverse))\n",
    "# print(embedding_matrix.shape[0])\n",
    "generate_reply(\"武汉加油\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  sentence\n",
      "0    武汉 加油\n",
      "[[18, 43]]\n"
     ]
    }
   ],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
